<?php
/**
 * Protoweb's models base Class
 *
 * This class provides some base functionality in addittion to CodeIgniter
 * and DMZ. The methods defined here are commonly used in any model.
 *
 * @abstract
 * @author Gonzalez Federico, Viera Leonel
 * @package Protoweb
 * @subpackage model
 */
abstract class PwModel extends DataMapper {

  /**
   * Class Constructor
   *
   * @param integer $id If set, constructor returns the an object with the data
   * of the matching id
   */
  public function __construct($id = NULL) {
    parent::__construct($id);
  } // end of method __construct($id = NULL)

  /**
   * Returns an asociative array compatible with {@link http://www.smarty.net/ smarty}.
   *
   * Returns an asociative array compatible with {@link http://www.smarty.net/ smarty} with the extructure:
   * <code>
   * array(
   *   key0 => value0
   *   key1 => value1
   *   key2 => value2
   * );
   * </code>
   *
   * @param string $key
   * @param string $value
   * @return array An asociative array
   */
  public function all_to_asoc_array($key, $value) {
    $arr = array();
    foreach($this->get()->all as $row)
      $arr[$row->$key] = $row->$value;
    return ($arr);
  } // end of method all_to_asoc_array($key, $value)

  /**
   *
   * Returns an array with all the $field data of all instances of this object
   *
   * An example: <code>array(0,1,2,3,4,5)</code>
   *
   * @param string $fields Field name to fill the returned array with
   * @return array
   */
  public function all_to_table($fields) {
    $arr = array();
    foreach($this->all as $row) {
      foreach($fields as $field)
        array_push($arr, $row->$field);
    }
    return $arr;
  } // public function all_to_table($fields)

  /**
   * Returns an array with the ids of the objects that form part of the
   * $rel_name relationship
   *
   * @param string $rel_name The name of the relationship to look for
   * @return array An array of ids
   */
  public function get_relationship_ids($rel_name) {
    $ids = array();

    foreach ($this->$rel_name as $rel) {
      array_push($ids, $rel->id);
    }

    return($ids);
  } // end of method get_relationship_ids($model_name)

  /**
   * Update the relationship $model_name of the object
   * 
   * This method deletes all the $model_name relationships of the object and then
   * update It with the newest data ($post_array).
   *
   * @param string $model_name The name of the ralationship to update
   * @param array $post_array An array with ids
   * @return Collection A collection with all the related objects
   */
  public function update_multiple_relations_with($model_name, $post_array) {
    // get related model
    $related_model = new $model_name();
    $model_name    = $related_model->model;

    // obtain old relations
    $old_relations = $this->$model_name->get()->all;

    // delete it
    $this->delete($old_relations);

    // get new relations
    if ($post_array) {
      foreach($post_array as $row_id) {
        $related_model->or_where('id', $row_id);
      }
      return ($related_model->get()->all);
    }
    else 
      return ($related_model);
  } // end of method update_multiple_relations_with($model_name, $post_array)
  
}// Class PwModel

/* End of file pwmodel.php */
/* Location: ./application/models/pwmodel.php */
